<template>
    <h2>会员信息添加页面</h2>
    <table border="1">
        <tr>
            <td>会员名称</td>
            <td>
                <input type="text" v-model="info.MI_Name"/>
            </td>
        </tr>
        <tr>
            <td>会员账号</td>
            <td>
                <input type="text" v-model="info.MI_Code"/>
            </td>
        </tr>
        <tr>
            <td>会员手机号</td>
            <td>
                <input type="text" v-model="info.MI_Phone"/>
            </td>
        </tr>
        <tr>
            <td>剩余金额</td>
            <td>
                <input type="text" v-model="info.MI_Price"/>
            </td>
        </tr>
        <tr>
            <td>会员积分</td>
            <td>
                <input type="text" v-model="info.MI_Score"/>
            </td>
        </tr>      
        <tr>
            <td>会员头像</td>
            <td>
                <img style="width:70px;height:70px" :src="info.MI_Img"/>
                <input type="file" @change="fileUpLoad" />
            </td>
        </tr>
        <tr>
            <td>会员性别</td>
            <td>
                <input type="radio" :value="true" v-model="info.MI_Sex"/>男
                <input type="radio" :value="false" v-model="info.MI_Sex"/>女
            </td>
        </tr>
        <tr>
            <td>会员生日</td>
            <td>
                <input type="date" v-model="info.MI_BirthDay"/>
            </td>
        </tr>
        <tr>
            <td>会员支付方式</td>
            <td>
                <input type="radio" value="现金" v-model="info.MI_PayMethod"/>现金
                <input type="radio" value="微信" v-model="info.MI_PayMethod"/>微信
                <input type="radio" value="支付宝" v-model="info.MI_PayMethod"/>支付宝
            </td>
        </tr>
        <tr>
            <td></td>
            <td>
                <input type="button" value="添加" @click="addInfo" />
            </td>
        </tr>
    </table>
</template>

<script setup lang="ts">
    import {ref} from 'vue'
    import {useRouter} from 'vue-router'
    const router=useRouter();
    import axios from 'axios'
    const file=ref(null);
    const fileUpLoad=(e)=>{
        file.value=e.target.files[0];
        if(!file.value)
        {
            alert('请上传文件');
            return;
        }
        const formData=new FormData();
        formData.append('image',file.value);
        axios.post('https://localhost:7292/api/File/FileUpLoad',formData)
        .then(res=>{
            if(res.data.FileCode==1)
            {
                alert(res.data.FileMessage);
            }
            else if(res.data.FileCode==2)
            {
                alert(res.data.FileMessage);
            }
            else
            {
                alert(res.data.FileMessage);
                info.value.MI_Img=res.data.FilePath;
            }
        })
    }

    const info=ref( {
      "MI_Id": 1,
      "MI_Name": "",
      "MI_Code": "",
      "MI_Phone": "",
      "MI_Price": null,
      "MI_Score": null,
      "MI_ScoreRemark": "",
      "MI_Img": "",
      "MI_Sex": true,
      "MI_BirthDay": "",
      "MI_PayMethod": "",
      "MI_PayRemark": ""
    })
    const addInfo=()=>{
        if(info.value.MI_Name=="")
        {
            alert('会员名称不能为空');
            return;
        }

        axios.post('https://localhost:7292/api/Member/InfoAdd',{
            "MI_Name": info.value.MI_Name,
      "MI_Code": info.value.MI_Code,
      "MI_Phone": info.value.MI_Phone,
      "MI_Price": info.value.MI_Price,
      "MI_Score": info.value.MI_Score,
      "MI_ScoreRemark": "",
      "MI_Img": info.value.MI_Img,
      "MI_Sex": info.value.MI_Sex,
      "MI_BirthDay": info.value.MI_BirthDay,
      "MI_PayMethod": info.value.MI_PayMethod,
      "MI_PayRemark": ""
        })
        .then(res=>{
            if(res.data==-1)
            {
                alert('会员名称不能重复');
            }
            else if(res.data>0)
            {
                alert('添加成功');
                router.push('/showInfo');
            }
            else
            {
                alert('添加失败');
            }
        })
    }

</script>